package com.uft.model.dao;

import java.util.List;

import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.uft.controller.ServerController;
import com.uft.model.entity.ConfigProperty;
import com.uft.model.entity.Profile;
import com.uft.model.entity.ProfilePreferences;
import com.uft.model.entity.Server;
import com.uft.model.entity.ServerRuntime;
import com.uft.model.entity.ServerSettings;

@Repository
public class ProfileDAO {
	@PersistenceContext private EntityManager em;
	private  static Logger  logger = Logger.getLogger(ProfileDAO.class);
	// Stores a new Profile:
   @Transactional
   public void persist(Profile profile) {
        em.persist(profile);
    }
   // Retrieves all the Profiles:
   public List<Profile> getAllProfiles() {
       TypedQuery<Profile> query = em.createQuery(
           "SELECT p FROM Profile p ", Profile.class);
       return query.getResultList();
   }
   public Profile getProfileById(int profileId){
	   logger.info("Profile Id: "+profileId);
	   Profile profile=em.find(Profile.class, profileId);
	   return profile;
   }
   public Profile getProfileByServerId(int serverId ){
	   return  (Profile) em.createNamedQuery("getProfileByServerId")
			    .setParameter("serverId", serverId)
			    .getSingleResult();
   }
   public int getProfileIdByServerId(int serverId ){
	   
	   return  getProfileByServerId(serverId).getProfileId();
   }
   
   @Transactional
   public void  addProfile(Profile profile){
	   logger.info("Profile name: "+profile.getProfileName());
	   em.persist(profile.getProfilePref());
	   em.persist(profile);
	   logger.info("Profile id: "+profile.getProfileId());
	  
   }
   @Transactional
	public void updateProfile(Profile profileBean) {
		// TODO Auto-generated method stub
	   logger.info("Profile name: "+profileBean.getProfileName());
	   em.merge(profileBean);
	   logger.info("Profile id: "+profileBean.getProfileId());
		
	}
   @Transactional
	public void updateProfilePreference(ProfilePreferences profilePref) {
		// TODO Auto-generated method stub
	   logger.info("Profile name: "+profilePref.getPrefId());
	   em.merge(profilePref);
		
	}
   @Transactional
   public void deleteProfileById(int profileId) {
	// TODO Auto-generated method stub
	   logger.info("Profile id: "+profileId);
	   Profile profile = em.find(Profile.class, profileId);
	   em.remove(profile);
	   
   }
   
  
   
   
   
   
}
